package 人事管理系统.Connect;

import 人事管理系统.Tables.Opdep;
import 人事管理系统.Tables.Salaryinfo;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.ArrayList;

public class Opdep_database {
    private  String ip=this.getIp();
    private String url="jdbc:mysql://"+ip+":3306/人事管理系统?useOldAliasMetadataBehavior=true";
    //获取ip
    public String getIp(){
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: "+hostname);
        return addr.getHostAddress();
    }
    /**********************************************************************************************************************/

    /*
   查询管理员对部门的操作记录
    */
    public ArrayList<Opdep> findAll()   {
        ArrayList<Opdep> list=new ArrayList<>();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM operate_dep ";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){
                //封装对象
                Opdep op=new Opdep();
                op.setChangeid(rs.getString("changeid"));
                op.setDno(rs.getString("DNO"));
                op.setOperate(rs.getString("Operate"));
                op.setOperatetime(rs.getString("operatetime"));
                list.add(op);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return list;
    }
}
